package tasks;

import java.util.LinkedList;

/**
 * Simulates "processes" and serves as a wrapper for threads. 
 * @author briandkid
 *
 */
public class Process {
	
	private int num_of_threads;
	
	private static int num_of_processes;
	
	private LinkedList<Thread> thread_list;
	
	public static final int MAX_NUMBER_THREADS = 5;
	
	
	private Process(){
		num_of_threads = 0;
		thread_list = new LinkedList<Thread>();
	}
	
	/**
	 * This method is called at the beginning of simulation
	 */
	public static void init(){
		num_of_processes = 0;
	}

	/**
	 * Creates a new process.
	 * 
	 * @return a new process
	 */
	public static Process create(){
		Process p = new Process();
		num_of_processes++;
		
		return p;
	}
	
	/**
	 * kills the process
	 */
	public void kill(){
		//kill all threads
		
	};
	
	/**
	 * Returns the number of threads in the process
	 * @return the number of threads
	 */
	public int thread_count(){
		return thread_list.size();
	}
	
	
	/**
	 * Adds a thread to the current process.
	 * 
	 * @param thread is the thread object to add
	 * @return true if the addition was successful. Otherwise, false is returned in case the maximum
	 * number of threads is exceeded.
	 */
	public boolean add_thread(Thread thread){
		if(thread_list.size() < MAX_NUMBER_THREADS - 1){
			thread_list.add(thread);
			return true;
		}
		return false;
	}
	
	/**
	 * Removes a thread from the process
	 * 
	 * @param thread is thread to remove
	 * @return true if it was removed successfully
	 */
	public boolean remove_thread(Thread thread){
		return thread_list.remove(thread);
	}
}
